/**
 * 文件上传控件初始化
 * @returns
 */
function f_upload_init(fileId){




	var submitObject = $("#"+fileId).parent().find("[submit='true']");
	var image_div = $("#"+fileId).parent().find(".ke-swfupload-body");
	$("#"+fileId).uploadify({
		'uploader'	:	"/heyinggoAdmin/plugin/uploadify/uploadify.swf",
		'script'    :	"/heyinggoAdmin/UploadController/systemDopostFileUpload",
		'cancelImg' :	"/heyinggoAdmin/plugin/uploadify/cancel.png",
		'folder'	:	"uploads",//上传文件存放的路径,请保持与uploadFile.jsp中PATH的值相同
		'queueId'	:	"fileQueue",
		'queueSizeLimit'	:	1000,//限制上传文件的数量
		'fileExt'	:	"*.rar,*.zip",
		//'fileDesc'	:	"RAR *.rar",//限制文件类型
		//'sizeLimit' : "1024",
		'auto'		:	true,
		'multi'		:	true,//是否允许多文件上传
		'simUploadLimit':	1,//同时运行上传的进程数量
		'buttonText':	"files",
		'buttonImg' :"/heyinggoAdmin/plugin/uploadify/atth.gif",
		//'hideButton': true,//设置为true则隐藏浏览按钮的图片
		'width'	:300,// ： 设置浏览按钮的宽度 ，默认值：110。
		'height'	:50,// ： 设置浏览按钮的高度 ，默认值：30。
		//wmode ： 设置该项为transparent 可以使浏览按钮的flash背景文件透明，并且flash文件会被置为页面的最高层。 默认值：opaque 。
		'scriptData':	{},//这个参数用于传递用户自己的参数，此时'method' 必须设置为GET, 后台可以用request.getParameter('name')获取名字的值
		'method'	:	"GET",
		'onSelect': function(e, queueId, fileObj)
		{
		},

		'onComplete': function(event, queueID, fileObj,serverData,data) {





			if(fileObj.name &&  fileObj.name.length>20){
				fileObj.name = fileObj.name.substring(0,20)+"...";
			}

			var j = eval("("+serverData+")");
			$.ajax({
				url : "/heyinggoAdmin/UploadController/get_atth_image?date="+new Date(),
				data : {path:j.filePath},
				type : "POST",
				success : function(text){
					$(image_div).append(text);
					var atth_items = $(image_div).find("[item='div_iamge']");
					//获取当前字段的所有图片
					$(submitObject).val("");
					for(var ar=0;ar<atth_items.length;ar++){
						var ts = $(submitObject).val();
						if(ts == ""){
							$(submitObject).val($(atth_items[ar]).attr("datavalue"));
						}else{
							$(submitObject).val(ts+","+$(atth_items[ar]).attr("datavalue"));
						}
					}

				}
			});

			var responseData =serverData.split('|');//因为后台返回内容中包含大量隐性字符  ,如空格等一系列字符 , 在后台已经设置,将返回的内容分隔在|| 之间

			responseData =responseData[1];
			//alert(responseData);

			var resp = "<DIV id='file_u_"+responseData+"'  class='uploadifyQueueItemX'><DIV class='cancel'>"+""+"<A href='javascript:deleteAtthFile("+responseData+")'><IMG border='0' src='http://39.106.218.58:8080/uploadify/cancel.png' /></A></DIV><SPAN class='fileName'><IMG border='0' src='http://39.106.218.58:8080/uploadify/atta.gif' />"+fileObj.name+" (100%)</SPAN> </DIV>";
			document.getElementById('resultFileDiv').innerHTML += resp;
			//alert(document.getElementById('resultFileDiv').innerHTML);

			addAtthMessage(responseData);


		}//当上传完成后的回调函数，ajax方式


	});

}


function addAtthMessage(atth_id){
	document.getElementById('atthIds').value =document.getElementById('atthIds').value+atth_id+",";

}
function removeAtthMessage(atth_id){
	document.getElementById('atthIds').value =document.getElementById('atthIds').value.replace(','+atth_id+",",",");
	alert(1);
}





function f_delete_atth(o){
	var id = $(o).attr("fileName");
	var ke_swfupload_body = $("#"+id).parent();
	$("#"+id).remove();

	var atth_items = $(ke_swfupload_body).find("[item='div_iamge']");
	var submitObject = $(ke_swfupload_body).parent().find("[submit='true']");


	//获取当前字段的所有图片
	$(submitObject).val("");
	for(var ar=0;ar<atth_items.length;ar++){
		var ts = $(submitObject).val();
		if(ts == ""){
			$(submitObject).val($(atth_items[ar]).attr("datavalue"));
		}else{
			$(submitObject).val(ts+","+$(atth_items[ar]).attr("datavalue"));
		}
	}
}




function f_set_atth_images(fileId,images_path){
	var image_div = $("#"+fileId).parent().find(".ke-swfupload-body");
	post("/admin/api/UploadController/get_atth_image_list",{path:images_path},function(msg){
		$(image_div).append(msg);
	});
}


var laydate;
function layuiFileUploadInit(){


	layui.use(["laydate","upload"],function(){
		laydate = layui.laydate;
		$("[t=date]").each(function (index,element) {
			let id = $(this).attr("id");
			let name = $(this).attr("name");
			let formData = $(this).attr("formData");
			//日期常规用法
			laydate.render({
				elem: "#"+id
				,done: function(value, date, endDate){
					console.log(value); //得到日期生成的值，如：2017-08-18
					console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
					console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
					vm[formData][name] = value;//设置vue数据对象的值
				}
			});
		});
		$("[t=datetime]").each(function (index,element) {
			let id = $(this).attr("id");
			let name = $(this).attr("name");
			let formData = $(this).attr("formData");
			//日期常规用法
			laydate.render({
				elem: "#"+id,
				type: 'datetime',
				done:function (value,date,endDate) {
					vm[formData][name] = value;//设置vue数据对象的值
				}
			});
		});


		var upload = layui.upload;

		$("[file=true]").each(function(index,element){
			var id = $(this).attr("id");
			var column = $(this).attr("column");

			//执行实例
			var uploadInst = upload.render({
				elem: '#'+id //绑定元素
				,url: '/admin/api/UploadController/systemDopostFileUpload' //上传接口
				,multiple:true,
				accept: 'file',
				headers: {
					secret:getSecret(),
					token:localStorage.getItem("token")
				},
				before:function(obj){
					this.headers.secret = getSecret();
				},
				progress: function(n, elem, res, index){
					var percent = n + '%' //获取进度百分比
					$(elem).text("文件上传"+percent);
					// console.log(percent);
					// console.log(elem); //得到当前触发的元素 DOM 对象。可通过该元素定义的属性值匹配到对应的进度条。
					// console.log(res); //得到 progress 响应信息
					// console.log(index); //得到当前上传文件的索引，多文件上传时的进度条控制，如：
				},
				done: function(res){
					var v = $("[name="+column+"]").val();
					if(v!=""){
						$("[name="+column+"]").val(v+","+res.data.src);
					}else{
						$("[name="+column+"]").val(res.data.src);
					}
					let src = res.data.src;
					vm.formData[column] = $("[name="+column+"]").val();
					console.log($("[name="+column+"]").val());
					var srcvalues = src.split(",");
					for(var a=0;a<srcvalues.length;a++){
						//上传完毕回调
						var delbutton = '<button type="button" column="'+column+'" onclick="f_delelete_file(this)"  src="'+srcvalues[a]+'" class="layui-btn layui-btn-sm layui-btn-normal"><i class="layui-icon"></i> 删除</button>';
						$("[trimages="+column+"]").append('<td style="padding: 10px;text-align: center;" tdcolumn="'+column+'" ><img src="'+srcvalues[a]+'"  style="padding-bottom: 10px;width:80px;height:80px;" width="80px" height="80px" ><br/>'+delbutton+'</td>');
					}

				}
				,error: function(){
					//请求异常回调
				}
			});
		});



	});


}



function f_delelete_file(o){
	var src = $(o).attr("src");
	var column = $(o).attr("column");

	var v = $("[name="+column+"]").val();
	var vs = v.split(",");
	for(var a=0;a<vs.length;a++){
		if(src == vs[a]){
			delete vs[a];
		}
	}


	var bsrc = "";
	for(var a=0;a<vs.length;a++){
		if(vs[a] && vs[a] != "undefined"){
			if(bsrc == ""){
				bsrc = vs[a];
			}else{
				bsrc = bsrc+","+vs[a];
			}
		}

	}

	$("[name="+column+"]").val(bsrc);
	$(o).parent().remove();
	vm.formData[column] = $("[name="+column+"]").val();

}






